home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / disk / misc / ADFlib.lha / Lib / adf_util.c < prev    next >
C/C++ Source or Header  |  1999-05-16  |  4KB  |  235 lines

  1. /*
  2.  *  ADF Library. (C) 1997-1998 Laurent Clevy
  3.  *
  4.  *  adf_util.c
  5.  *
  6.  */
  7.  
  8. #include<stdlib.h>
  9. #include<time.h>
  10.  
  11. #include "adf_util.h"
  12. #include "adf_err.h"
  13.  
  14. extern struct Env adfEnv;
  15.  
  16.  
  17. /*
  18.  * swLong
  19.  *
  20.  * write an unsigned long value (val) (in) 
  21.  * to an unsigned char* buffer (buf) (out)
  22.  * 
  23.  * used in adfWrite----Block() functions
  24.  */
  25. void swLong(unsigned char* buf, unsigned long val)
  26. {
  27.     buf[0]= (unsigned char)((val & 0xff000000) >>24UL);
  28.     buf[1]= (unsigned char)((val & 0x00ff0000) >>16UL);
  29.     buf[2]= (unsigned char)((val & 0x0000ff00) >>8UL);
  30.     buf[3]= (unsigned char)(val & 0x000000ff);
  31. }
  32.  
  33. void swShort(unsigned char* buf, unsigned short val)
  34. {
  35.     buf[0]= (val & 0xff00) >>8UL;
  36.     buf[1]= (val & 0x00ff) ;
  37. }
  38.  
  39. /*
  40.  * newCell
  41.  *
  42.  * adds a cell at the end the list
  43.  */
  44. struct List* newCell(struct List* list, void* content)
  45. {
  46.     struct List* cell;
  47.  
  48.     cell=(struct List*)malloc(sizeof(struct List));
  49.     if (!cell) {
  50.         (*adfEnv.eFct)("newCell : malloc");
  51.         return NULL;
  52.     }
  53.     cell->content = content;
  54.     cell->next = cell->subdir = 0;
  55.     if (list!=NULL)
  56.         list->next = cell;
  57.  
  58.     return cell;
  59. }
  60.  
  61.  
  62. /*
  63.  * freeList
  64.  *
  65.  */
  66. void freeList(struct List* list)
  67. {
  68.     if (list==NULL) 
  69.         return;
  70.     
  71.     if (list->next)
  72.         freeList(list->next);
  73.     free(list);
  74. }
  75.  
  76.  
  77.  
  78.  
  79. /*
  80.  * Days2Date
  81.  *
  82.  * amiga disk date format (days) to normal dd/mm/yy format (out)
  83.  */
  84.  
  85. void 
  86. adfDays2Date(long days, int *yy, int *mm, int *dd)
  87. {
  88.     int y,m;
  89.     int nd;
  90.     int jm[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  91.  
  92.     /* 0 = 1 Jan 1978,  6988 = 18 feb 1997 */
  93.  
  94.     /*--- year ---*/
  95.     y=1978;
  96.     if (adfIsLeap(y))
  97.         nd=366;
  98.     else
  99.         nd=365;
  100.     while( days >= nd ) {
  101.         days-=nd;
  102.         y++;
  103.         if (adfIsLeap(y))
  104.             nd=366;
  105.         else
  106.             nd=365;
  107.     }
  108.  
  109.  
  110.     /*--- month ---*/
  111.     m=1;
  112.     if (adfIsLeap(y))
  113.         jm[2-1]=29;
  114.     while( days >= jm[m-1] ) {
  115.         days-=jm[m-1];
  116.         m++;
  117.     }
  118.  
  119.     *yy=y;
  120.     *mm=m;
  121.     *dd=days+1;
  122. }
  123.  
  124.  
  125. /*
  126.  * IsLeap
  127.  *
  128.  * true if a year (y) is leap
  129.  */
  130.  
  131.     BOOL 
  132. adfIsLeap(int y)
  133. {
  134.     return( (BOOL) ( !(y%100) ? !(y%400) : !(y%4) ) );
  135. }
  136.  
  137.  
  138. /*
  139.  * adfCurrentDateTime
  140.  *
  141.  * return the current system date and time
  142.  */
  143.     struct DateTime
  144. adfGiveCurrentTime( void )
  145. {
  146.     struct tm *local;
  147.     time_t cal;
  148.     struct DateTime r;
  149.  
  150.     time(&cal);
  151.     local=localtime(&cal);
  152.  
  153.     r.year=local->tm_year;         /* since 1900 */
  154.     r.mon=local->tm_mon+1;
  155.     r.day=local->tm_mday;
  156.     r.hour=local->tm_hour;
  157.     r.min=local->tm_min;
  158.     r.sec=local->tm_sec;
  159.  
  160.     return(r);
  161. }
  162.  
  163.  
  164. /*
  165.  * adfTime2AmigaTime
  166.  *
  167.  * converts date and time (dt) into Amiga format : day, min, ticks
  168.  */
  169.     void
  170. adfTime2AmigaTime(struct DateTime dt, long *day, long *min, long *ticks )
  171. {
  172.     int jm[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  173.  
  174.  
  175.     *min= dt.hour*60 + dt.min;                /* mins */
  176.     *ticks= dt.sec*50;                        /* ticks */
  177.  
  178.     /*--- days ---*/
  179.  
  180.     *day= dt.day-1;                         /* current month days */
  181.  
  182.     /* previous months days downto january */
  183.     if (dt.mon>1) {                      /* if previous month exists */
  184.         dt.mon--;
  185.         if (dt.mon>2 && adfIsLeap(dt.year))    /* months after a leap february */
  186.             jm[2-1]=29;
  187.         while(dt.mon>0) {
  188.             *day=*day+jm[dt.mon-1];
  189.             dt.mon--;
  190.         }
  191.     }
  192.  
  193.     /* years days before current year downto 1978 */
  194.     if (dt.year>78) {
  195.         dt.year--;
  196.         while(dt.year>=78) {
  197.             if (adfIsLeap(dt.year))
  198.                 *day=*day+366;
  199.             else
  200.                 *day=*day+365;
  201.             dt.year--;
  202.         }
  203.     }
  204. }
  205.  
  206.  
  207. /*
  208.  * dumpBlock
  209.  *
  210.  * debug function : to dump a block before writing the check its contents
  211.  *
  212.  */
  213. void dumpBlock(unsigned char *buf)
  214. {
  215.     int i, j;
  216.  
  217.     for(i=0; i<32; i++) {
  218.         printf("%5x ",i*16);
  219.         for (j=0; j<4; j++) {
  220.             printf("%08x ",Long(buf+j*4+i*16));
  221.         }
  222.         printf("    ");
  223.         for (j=0; j<16; j++)
  224.             if (buf[i*16+j]<32 || buf[i*16+j]>127)
  225.                 putchar('.');
  226.             else
  227.                 putchar(buf[i*16+j]);
  228.         putchar('\n');
  229.     }
  230. }
  231.  
  232.  
  233.  
  234. /*################################################################################*/
  235.